package com.diankangwo.controller;

import com.diankangwo.constant.resultMsg;
import com.diankangwo.dao.*;
import com.diankangwo.domain.*;
import com.diankangwo.dto.DiscussDTO;
import com.diankangwo.util.PageSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.sql.Timestamp;
import java.util.*;

/**
 * 评论控制器
 *
 * @author xiaoyu
 */
@Controller
@Api(description = "评论")
public class DiscussController {

    @Autowired
    private UserDAO userDao;

    @Autowired
    private CommodityDAO commDao;

    @Autowired
    private DiscussDAO disDao;

    @Autowired
    private DiscussReplyDAO crDao;

    @Autowired
    private OrderDAO orderDAO;


    /**
     * 获取商品评论列表
     *
     * @param accessToken token
     * @param id          商品ID
     * @return
     */
    @ApiOperation(value = "获取某条评论的回复列表", notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "accessToken", value = "token", required = true, dataType = "String"),
            @ApiImplicitParam(name = "id", value = "商品ID", required = true, dataType = "Integer")
    })
    @ResponseBody
    @RequestMapping(value = "/comment/discuss", method = RequestMethod.GET)
    public String discuss(int pageNo, int pageSize, Integer id, int type) {
        String code = "2";
        String msg = "处理异常！";
        PageSupport<Discuss> cts = null;
        List<DiscussDTO> discussList = new ArrayList<>();
        cts = disDao.getUserByUid(id, pageNo, pageSize, type);
        if (cts != null) {
            for (int i = 0; i < cts.getResultData().size(); i++) {
                DiscussDTO dd = new DiscussDTO();
                dd.setAdd_date(cts.getResultData().get(i).getAdd_date());
                dd.setClickPraise(cts.getResultData().get(i).getClickPraise());
                dd.setCommodityId(cts.getResultData().get(i).getCommodityId());
                dd.setContent(cts.getResultData().get(i).getContent());
                dd.setEvaluate(cts.getResultData().get(i).getEvaluate());
                dd.setId(cts.getResultData().get(i).getId());
                dd.setIscheck(cts.getResultData().get(i).isCheck());
                dd.setPic(cts.getResultData().get(i).getPic());
                dd.setPraiseNum(cts.getResultData().get(i).getPraiseNum());
                dd.setReply(cts.getResultData().get(i).isReply());
                dd.setReplyId(cts.getResultData().get(i).getReplyId());
                dd.setReplyNumber(cts.getResultData().get(i).getReplyNumber());
                dd.setUserInfo(userDao.find(cts.getResultData().get(i).getUserId()));
                discussList.add(dd);
            }
            code = "1";
            msg = "查询成功！";
        } else {
            code = "1";
            msg = "未查询到评价信息！";
        }
        return resultMsg.mapToJson(code, msg, discussList);
    }


    /**
     * 评价某个商品
     *
     * @param accessToken
     * @param commodityID 商品ID
     * @param commentType 评价类型:差评,好评,中评
     * @param pic         评价图片地址
     * @param content     评价内容
     * @return
     */
    @ApiOperation(value = "评价某个商品", notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "accessToken", value = "token", required = true, dataType = "String"),
            @ApiImplicitParam(name = "commodityID", value = "商品ID", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "commentType", value = "评价类型:差评,好评,中评", required = true, dataType = "String"),
            @ApiImplicitParam(name = "pic", value = "评价图片地址", required = true, dataType = "String"),
            @ApiImplicitParam(name = "content", value = "评价内容", required = true, dataType = "String"),
            @ApiImplicitParam(name = "reply", value = "类型 1:回复评论 0：评论", required = true, dataType = "Boolean"),
            @ApiImplicitParam(name = "id", value = "评论ID", required = true, dataType = "Integer")
    })
    @ResponseBody
    @RequestMapping(value = "/comment", method = RequestMethod.POST)
    public String comment(String accessToken, int commodityID, int commentType, String pic,
                          String content, int reply, int id, String orderNo) {
        UserInfo user = userDao.getUserByToken(accessToken);
        String code = "2";
        String msg = "处理异常！";
        if (user != null) {
            Discuss cots = new Discuss();
            Date date = new Date();
            Timestamp nousedate = new Timestamp(date.getTime());
            cots.setAdd_date(nousedate);
            cots.setCommodityId(commDao.find(commodityID).getId());
            cots.setContent(content);
            cots.setClickPraise(1);
            cots.setPic(pic);
            cots.setUserId(user.getId());
            if (reply == 1) {
                cots.setReplyId(id);
            } else {
                cots.setReplyId(0);
            }
            cots.setReply(reply);
            cots.setReplyNumber(1);
            cots.setEvaluate(commentType);
            cots.setPraiseNum(0);
            if (disDao.saveOrUpdate(cots) != null) {
                //修改订单状态
                Order order = orderDAO.findByOrderNo(orderNo);
                order.setOrderStatus("success");
                if (orderDAO.update(order) != null) {
                    code = "1";
                    msg = "添加成功！";
                }
            }
        } else {
            msg = "未查询到用户信息！";
        }
        return resultMsg.mapToJson(code, msg, null);
    }

    /**
     * 获取某条评论的回复列表
     *
     * @param accessToken token
     * @param id          评论ID
     * @return
     */
    @ApiOperation(value = "获取某条评论的回复列表", notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "accessToken", value = "token", required = true, dataType = "String"),
            @ApiImplicitParam(name = "id", value = "评论ID", required = true, dataType = "Integer")
    })
    @ResponseBody
    @RequestMapping(value = "/comment/reply", method = RequestMethod.GET)
    public String reply(String accessToken, Integer id) {
        String code = "2";
        String msg = "处理异常！";
        List<Discuss> cts = new ArrayList<>();
        if (accessToken == "" || id == null) {
            msg = "数据不完整!";
        } else {
            UserInfo user = userDao.getUserByToken(accessToken);
            if (user != null) {
                cts = disDao.findPagination("from Discuss where replyId = ?", 1, Integer.MAX_VALUE, id).getResultData();
                if (cts != null) {
                    code = "1";
                    msg = "查询成功！";
                } else {
                    code = "1";
                    msg = "未查询到评价信息！";
                }
            } else {
                msg = "未查询到用户信息！";
            }
        }
        return resultMsg.mapToJson(code, msg, cts);
    }

    /**
     * 回复某条评论
     *
     * @param accessToken token
     * @param id          评论ID
     * @param content     内容
     * @return
     */
    @ApiOperation(value = "回复某条评论", notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "accessToken", value = "token", required = true, dataType = "String"),
            @ApiImplicitParam(name = "id", value = "评论ID", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "content", value = "评论内容", required = true, dataType = "String")
    })
    @ResponseBody
    @RequestMapping(value = "/comment/reply", method = RequestMethod.POST)
    public String reply(String accessToken, int id, String content) {
        UserInfo user = userDao.getUserByToken(accessToken);
        String code = "2";
        String msg = "处理异常！";
        Discuss cts = disDao.find(id);
        if (user != null) {
            if (cts != null) {
                Discuss cr = new Discuss();
                Date date = new Date();
                Timestamp nousedate = new Timestamp(date.getTime());
                cr.setAdd_date(nousedate);
                cr.setContent(content);
                cr.setReplyId(id);
                cr.setReply(1);
                cr.setUserId(user.getId());
                cts.setReplyNumber(cts.getReplyNumber() + 1);
                if (disDao.saveOrUpdate(cr) != null) {
                    disDao.update(cts);
                    code = "1";
                    msg = "回复成功！";
                } else {
                    msg = "系统异常，回复失败!";
                }
            } else {
                code = "1";
                msg = "未查询到评价信息！";
            }
        } else {
            msg = "未查询到用户信息！";
        }
        return resultMsg.mapToJson(code, msg, null);
    }

    /**
     * 统计
     *
     * @param id 商品ID
     * @return
     */
    @ApiOperation(value = "统计", notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "商品ID", required = true, dataType = "Integer")
    })
    @ResponseBody
    @RequestMapping(value = "/comment/total", method = RequestMethod.GET)
    public String total(Integer id) {
        String code = "2";
        String msg = "处理异常！";
        Map<String, Integer> map = new HashMap<>();
        PageSupport<Discuss> cts = disDao.getUserByUid(id, 1, Integer.MAX_VALUE, 1);
        if (cts != null) {
            map.put("1", cts == null ? null : cts.getResultData().size());
            cts = disDao.getUserByUid(id, 1, Integer.MAX_VALUE, 2);
            map.put("2", cts == null ? null : cts.getResultData().size());
            cts = disDao.getUserByUid(id, 1, Integer.MAX_VALUE, 3);
            map.put("3", cts == null ? null : cts.getResultData().size());
            cts = disDao.getUserByUid(id, 1, Integer.MAX_VALUE, 4);
            map.put("4", cts == null ? null : cts.getResultData().size());
            cts = disDao.getUserByUid(id, 1, Integer.MAX_VALUE, 5);
            map.put("all", cts == null ? null : cts.getResultData().size());
        }
        if (map != null) {
            code = "1";
            msg = "查询成功！";
        } else {
            code = "1";
            msg = "未查询到评价信息！";
        }
        return resultMsg.mapToJson(code, msg, map);
    }

    /**
     * 单个评论详情
     *
     * @param id 商品ID
     * @return
     */
    @ApiOperation(value = "单个评论详情", notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "评论ID", required = true, dataType = "Integer")
    })
    @ResponseBody
    @RequestMapping(value = "/comment/ById", method = RequestMethod.GET)
    public String ById(Integer id) {
        String code = "2";
        String msg = "处理异常！";
        Discuss discuss = disDao.find(id);
        DiscussDTO dd = new DiscussDTO();
        if (discuss != null) {
            dd.setAdd_date(discuss.getAdd_date());
            dd.setClickPraise(discuss.getClickPraise());
            dd.setCommodityId(discuss.getCommodityId());
            dd.setContent(discuss.getContent());
            dd.setEvaluate(discuss.getEvaluate());
            dd.setId(discuss.getId());
            dd.setIscheck(discuss.isCheck());
            dd.setPic(discuss.getPic());
            dd.setPraiseNum(discuss.getPraiseNum());
            dd.setReply(discuss.isReply());
            dd.setReplyId(discuss.getReplyId());
            dd.setReplyNumber(discuss.getReplyNumber());
            dd.setUserInfo(userDao.find(discuss.getUserId()));
            code = "1";
            msg = "查询成功！";
        } else {
            code = "1";
            msg = "未查询到数据！";
        }
        return resultMsg.mapToJson(code, msg, dd);
    }

    /**
     * 单个评论回复列表
     *
     * @param id 评论ID
     * @return
     */
    @ApiOperation(value = "单个评论回复列表", notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "评论ID", required = true, dataType = "Integer")
    })
    @ResponseBody
    @RequestMapping(value = "/comment/replay", method = RequestMethod.GET)
    public String replay(Integer id, int pageNo, int pageSize) {
        String code = "2";
        String msg = "处理异常！";
        PageSupport<Discuss> discussPageSupport = disDao.findPagination("from Discuss where replyId=?", pageNo, pageSize, id);
        PageSupport<DiscussDTO> discussDTOPageSupport = new PageSupport<DiscussDTO>();
        if (discussPageSupport != null) {
            for (int i = 0; i < discussPageSupport.getResultData().size(); i++) {
                DiscussDTO dd = new DiscussDTO();
                dd.setAdd_date(discussPageSupport.getResultData().get(i).getAdd_date());
                dd.setClickPraise(discussPageSupport.getResultData().get(i).getClickPraise());
                dd.setCommodityId(discussPageSupport.getResultData().get(i).getCommodityId());
                dd.setContent(discussPageSupport.getResultData().get(i).getContent());
                dd.setEvaluate(discussPageSupport.getResultData().get(i).getEvaluate());
                dd.setId(discussPageSupport.getResultData().get(i).getId());
                dd.setIscheck(discussPageSupport.getResultData().get(i).isCheck());
                dd.setPic(discussPageSupport.getResultData().get(i).getPic());
                dd.setPraiseNum(discussPageSupport.getResultData().get(i).getPraiseNum());
                dd.setReply(discussPageSupport.getResultData().get(i).isReply());
                dd.setReplyId(discussPageSupport.getResultData().get(i).getReplyId());
                dd.setReplyNumber(discussPageSupport.getResultData().get(i).getReplyNumber());
                dd.setUserInfo(userDao.find(discussPageSupport.getResultData().get(i).getUserId()));
                discussDTOPageSupport.getResultData().add(dd);
            }
            code = "1";
            msg = "查询成功！";
        } else {
            code = "1";
            msg = "未查询到数据！";
        }
        return resultMsg.mapToJson(code, msg, discussDTOPageSupport);
    }
}
